/**
 * Created by lixinkun on 2017/4/20.
 */
'use strict';
const HOME_DATA = '@HomeData';

import {AsyncStorage} from 'react-native';

export default class HomeDataDAO{

    save(json, time){
        let data = {
            time: time,
            content: json
        };
        try {
            AsyncStorage.setItem(HOME_DATA, JSON.stringify(data));
        } catch (error) {

        }
    }

    fetchLocalData(time){
        return new Promise((resolve, reject) => {
            AsyncStorage.getItem(HOME_DATA, (error, result) =>{
                if (!error) {
                    const data = JSON.parse(result);
                    if (data) {
                        if (data.time.toString() === time) {
                            resolve(data.content);
                        } else {
                            reject(data.content);
                        }
                    } else {
                        reject(null);
                    }

                } else {
                    reject(null);
                }
            })
        })
    }
}